﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XYS;
using XYS.MISS.Photo.PhotoAdmin;
using System.Data;
using System.Configuration;
using System.Threading;
using LEDShow.Helpers;

namespace LEDShow.UC
{
    /// <summary>
    /// UCWeather.xaml 的交互逻辑
    /// </summary>
    public partial class UCWeather : UserControl
    {

        private Access.MainAccess _mainAccess;

        private int LineID = -1;

        /// <summary>
        /// 更新天气的线程
        /// </summary>
        private Thread UpdateWeatherThread = null;

        public UCWeather()
        {

            InitializeComponent();

            try
            {

                LineID = Convert.ToInt32(ConfigurationManager.AppSettings["LineID"]);
                _mainAccess = new Access.MainAccess(LineID);
                //启动更新天气的线程
                InitWeatherThread();


            }
            catch
            {

            }

        }

        private void InitWeatherThread()
        {

            if (null == UpdateWeatherThread)
            {
                UpdateWeatherThread = new Thread(new ThreadStart(RefreshWeather));
                UpdateWeatherThread.SetApartmentState(ApartmentState.STA);
                UpdateWeatherThread.IsBackground = true;
            }

            UpdateWeatherThread.Start();

        }

        /// <summary>
        /// 更新天气的线程
        /// </summary>
        public void RefreshWeather()
        {

            while (true)
            {
                try
                {
                    DataTable dtWeather = _mainAccess.GetWeatherInfo();
                    if (dtWeather.Rows.Count < 1)
                    {
                        return;
                    }
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        this.tbk_cityName.Text = dtWeather.Rows[0]["Address"].ToString();
                        this.tbk_holiday.Text = dtWeather.Rows[0]["holiday"].ToString();
                        this.tb_detail.Text = dtWeather.Rows[0]["Remark"].ToString();
                        this.tb_qw.Text = dtWeather.Rows[0]["Temperature"].ToString();
                        this.tb_fl.Text = dtWeather.Rows[0]["WindPower"].ToString();
                        this.tb_tq.Text = dtWeather.Rows[0]["Weather"].ToString();

                        //显示天气图片(目前设置11种常见天气）
                        string realPath = null;
                        string weather = dtWeather.Rows[0]["Weather"].ToString();
                        //string weather = "阴霾";
                        if (weather.Contains("晴转多云"))
                        {
                            realPath = string.Format("/LEDShow;component/Images/weathers/晴转多云.png");
                        }
                        else if (weather.Contains("晴"))
                        {
                            realPath = string.Format("/LEDShow;component/Images/weathers/晴.png");
                        }
                        else if (weather.Contains("多云") || weather.Contains("云"))
                        {
                            realPath = string.Format("/LEDShow;component/Images/weathers/多云.png");
                        }
                        else if (weather.Contains("暴雨") || weather.Contains("中雨"))
                        {
                            realPath = string.Format("/LEDShow;component/Images/weathers/暴雨.png");
                        }
                        else if (weather.Contains("雷阵雨"))
                        {
                            realPath = string.Format("/LEDShow;component/Images/weathers/雷阵雨.png");
                        }
                        else if (weather.Contains("阵雨"))
                        {
                            realPath = string.Format("/LEDShow;component/Images/weathers/阵雨.png");
                        }
                        else if (weather.Contains("小雨") || weather.Contains("雨"))
                        {
                            realPath = string.Format("/LEDShow;component/Images/weathers/小雨.png");
                        }
                        else if (weather.Contains("阴霾"))
                        {
                            realPath = string.Format("/LEDShow;component/Images/weathers/阴霾.png");
                        }
                        else if (weather.Contains("阴天") || weather.Contains("阴"))
                        {
                            realPath = string.Format("/LEDShow;component/Images/weathers/阴天.png");
                        }
                        else if (weather.Contains("大雪"))
                        {
                            realPath = string.Format("/LEDShow;component/Images/weathers/大雪.png");
                        }
                        else if (weather.Contains("小雪") || weather.Contains("雪"))
                        {
                            realPath = string.Format("/LEDShow;component/Images/weathers/小雪.png");
                        }
                        else
                        {
                            realPath = null;
                        }
                        //如果存在特定天气的图片，则加载图片，否则不加载图片
                        if (realPath != null)
                        {
                            this.WeatherPicture.Source = new BitmapImage(new Uri(realPath, UriKind.RelativeOrAbsolute));
                        }
                    }));

                }
                catch (Exception ex)
                {
                    Helpers.LEDShowGlobalvariables.globalvariables.log.writeError(ex.ToString());
                }

                Thread.Sleep(CONSTDATA.UpdateWeatherTime);
            }
        }
    }
}
